<?php
class Admin_MenuController extends Zend_Controller_Action{
	public function init(){
		
	}
	
	public function indexAction(){
		$mdlMenu =  new Admin_Model_Menu();
		$this->view->menus = $mdlMenu->getMenus();
	}
	
	public function createAction(){
		$frmMenu = new Form_Menu();
		if($this->getRequest()->isPost()){
			if($frmMenu->isValid($_POST)){
				$menuName = $frmMenu->getValue('name');
				$mdlMenu = new Admin_Model_Menu();
				$result = $mdlMenu->createMenu($menuName);
				if($result){
					$this->_redirect('/Menu/index');
				}
			}
		}
		$frmMenu->setAction('/Menu/Create');
		$this->view->form =  $frmMenu;
	}
	
	public function editAction(){
		$id = $this->request->getParam('id');
		$mdlMenu = new Admin_Model_Menu();
		$frmMenu = new Form_Menu();
		
		if($this->getRequest()->isPost()){
			if($frmMenu->isValid($_POST)){
				$menuName = $frmMenu->getValue('name');
				$mdlMenu = new Admin_Model_Menu();
				$result = $mdlMenu->updateMenu($id, $menuName);
				if($result){
					return $this->_forward('index');
				}
			}
		}else{
			$currentMenu = $mdlMenu->find($id)->current();
			$frmMenu->getElement('id')->setValue($currentMenu->id);
			$frmMenu->getElement('name')->setValue($currentMenu->name);
		}
		$frmMenu->setAction('/Menu/edit');
		$this->view->form = $frmMenu;
	}
	
	public function deleteAction(){
		$id = $this->_request->getParam('id');
		$mdlmenu = new Admin_Model_Menu();
		$mdlmenu->deleteMenu($id);
		$this->_forward('index');
	}
	
	public function renderAction(){
		$menu = $this->_request->getParam('menu');
		$mdlMenuItems = new Model_MenuItem();
		$menuItem = $mdlMenuItems->getItemsByMenu($menu);
		
		if(count($mdlMenuItems) > 0){
			foreach ($menuItem as $item){
				$label = $item->label;
				if(!empty($item->linl)){
					$uri = $item->link;
				}else {
					$page = new CMS_Content_item_Page($item->page_id);
					$uri = '/page/open/id/' . $item->page_id;
				}
				$itemArray[] = array(
										'label' => $label,
										'uri' => $uri
				);
			}
			$container = new Zend_Navigation($itemArray);
			$this->view-navigation()->setContainer($container);
		}
	}
}
?>